package com.xinfu.newproject.presenter;

import com.xinfu.newproject.R;
import com.xinfu.newproject.contants.Contants;
import com.xinfu.newproject.bean.home.HomeAdapterBean;
import com.xinfu.newproject.view.HomeAdapterView;

import java.util.ArrayList;

public class HomeAdapterPresenter {
    //实例model
    HomeAdapterModel homeAdapterModel;
    //实例view
    HomeAdapterView homeAdapterView;

    //构造方法
    public HomeAdapterPresenter(HomeAdapterView homeAdapterView) {
        //view接口传过来他就持有MainActivityView,然后定义出来,构造
        this.homeAdapterView = homeAdapterView;
        this.homeAdapterModel = new HomeAdapterModel();
    }

    //获取数据的方法,上拉刷新和下拉加载
    public void onfresh(boolean up,int type) {
        homeAdapterModel.onfresh(up, new HomeAdapterModel.ModelCallBack() {
            @Override
            public void success(ArrayList<HomeAdapterBean> homeAdapterBean) {
                //通过view的接口往回调
                homeAdapterView.success(homeAdapterBean);
            }

            @Override
            public void failure(Exception e) {
                homeAdapterView.fasle(e);
            }
        },type);

    }

//    public void onfreshTwo(boolean up,int type) {
//        homeAdapterModel.onfresh(up, new HomeAdapterModel.ModelCallBack() {
//            @Override
//            public void success(ArrayList<HomeAdapterBean> homeAdapterBean) {
//                //通过view的接口往回调
//                homeAdapterView.success(homeAdapterBean);
//            }
//
//            @Override
//            public void failure(Exception e) {
//                homeAdapterView.fasle(e);
//            }
//        },type);
//
//    }
    //上拉加载需要一个page参数
    /*public void onloadmore(int page){

    }*/


    public static class HomeAdapterModel {
        private int[] imgList = {R.mipmap.water_icon, R.mipmap.electricity_icon, R.mipmap.visitors_icon, R.mipmap.charging_icon};
        private int[] imgListUserTwo = {R.mipmap.payment_records_icon, R.mipmap.visitor_records_icon, R.mipmap.decoration_application_icon, R.mipmap.household_certification_icon};
        private String[] imgTextList = {"水费充值", "电费充值", "访客邀约", "充电桩"};
        private String[] imgTextListUserThree = {"缴费记录", "访客记录", "装修申请", "住户认证"};
        private ArrayList<HomeAdapterBean> list;
        //提供对应的上拉下拉方法
        //up为true就是下拉刷新，为false就是上拉加载
        public void onfresh(boolean up, final ModelCallBack modelCallBack,int type) {
//            //拿封装好的网络请求数据,第一个参数map类型，不需要，第二个，请求路劲,第三个回调，泛型是你的bean指定类型
//            OkhttpUtils.getInstance().asy(null, "http://v.juhe.cn/toutiao/index?type=top&key=597b4f9dcb50e051fd725a9ec54d6653", new AbstractUiCallBack<MyBean>() {
//                @Override
//                public void success(MyBean myBean) {
//                    //成功就调到数据了，然后要回调到presenter里
//                    modelCallBack.success(myBean);
//                }
//
//                @Override
//                public void failure(Exception e) {
//                    modelCallBack.failure(e);
//                }
//            });
            list = new ArrayList<>();

            if (type == Contants.HOMEDATA){
                list.clear();
                for (int i = 0; i < imgList.length; i++) {
                    HomeAdapterBean homeAdapterBean = new HomeAdapterBean();
                    homeAdapterBean.setImgView(imgList[i]);
                    homeAdapterBean.setImgText(imgTextList[i]);
                    list.add(homeAdapterBean);
                }

            }else if (type == Contants.USERTWODATA){
                list.clear();
                for (int i = 0; i < imgListUserTwo.length; i++) {
                    HomeAdapterBean homeAdapterBean = new HomeAdapterBean();
                    homeAdapterBean.setImgView(imgListUserTwo[i]);
                    homeAdapterBean.setImgText(imgTextListUserThree[i]);
                    list.add(homeAdapterBean);
                }
            }

            modelCallBack.success(list);


        }


        //创建回调接口
        public interface ModelCallBack {
            public void success(ArrayList<HomeAdapterBean> homeAdapterBean);

            public void failure(Exception e);
        }
        //需要判断是上拉还是下拉，就需要写两个方法，给个boon值用来判断,up为true是
    /*public void onloadmore(int page){

    }*/
    }
}
